//index.js
//获取应用实例
const app = getApp();
const valid = require("../../utils/valid.js");
const net = require("../../utils/net.js");
var posting = false;
Page({
  data: {
    hidden: true,
    tipmsg: "",
    tipno: 0,
    mobile: "",
    pswold: "",
    pswnew: "",
    pswnewconfirm: "",
    disable: false,
    countdown: 60,
    btnmsg: "获取验证码",
    mobiletip: 0,
    codetip:0,
    pswnewtip: 0,
    pswnewconfirmtip: 0,
    shop: [],
    shopids: [],
    shopindex: 0
  },
  changeShop(e){
    this.setData({
      shopindex: e.detail.value
    })
  }, 
  getshoplist(){
    var that = this;
    wx.showLoading({});
    net.request({
      url: '/GetShopList',
      data: {
        mobile: that.data.mobile
      }, success(res) {
        var dt = res.data;
        if (dt.flag) {
          var ids = [], shops = [];
          for (let i = 0, len = dt.data.length; i < len; ++i) {
            ids.push(dt.data[i]["id"])
            shops.push(dt.data[i]["shop_name"])
          }
          that.setData({
            shop: shops,
            shopids: ids
          })
        } else {
          that.showtip(2, dt.msg);
        }
      }, fail() {
        that.showtip(2, "系统错误，请联系技术人员");
      }, complete() {
        wx.hideLoading();
      }
    })
  },
  inputtip(val, key) {
    this.setData({
      [key]: val
    })
  },
  input: function (e) {
    var value = e.detail.value + "";
    var key = e.currentTarget.id + "";
    if (key == "mobile") {
      if (value.length == 11) {
        if (value.length >= 6)
          this.inputtip(2, "mobiletip");
        else
          this.inputtip(0, "mobiletip"); 
      }
    } else if (key == "code") {
      if (value.length >= 5)
        this.inputtip(2, "codetip");
      else
        this.inputtip(0, "codetip");
    } else if (key == "pswnew") {
      if (value.length >= 6)
        this.inputtip(2, "pswnewtip");
      else
        this.inputtip(0, "pswnewtip");
    } else if (key == "pswnewconfirm") {
      if (value.length >= 6 && value == this.data.pswnew)
        this.inputtip(2, "pswnewconfirmtip");
      else
        this.inputtip(0, "pswnewconfirmtip");
    }
    this.setData({
      [key]: value,
    });
    if (key == "mobile") {
      if (value.length == 11)
        this.getshoplist();
      else
        this.setData({
          shopids:[],
          shop:[]
        })
    }
  },
  showtip(lv, msg) {
    this.setData({
      tipno: lv,
      tipmsg: msg
    })
  },
  getcode() {
    var that = this;
    if (posting) return;
    posting = true;
    console.log("posting" + posting);
    valid.get_code(
      that.data.mobile, that.data.shopids[that.data.shopindex],
      {
        success(res) {
          that.setData({
            disable: true
          });
          that.showtip(0, "");
          that.cd();
        }, fail(code, msg) {
          that.showtip(code, msg);
        }, complete() {
        }
      })
    setTimeout(function () {
      posting = false;
    }, 1000);
  },
  cd() {
    var msg;
    var that = this;
    var time = this.data.countdown
    if (time <= 0) {
      msg = "重新发送";
    } else {
      msg = this.data.countdown + "s";
    }
    this.setData({
      btnmsg: msg,
      countdown: this.data.countdown-1
    });
    if (time <= 0) {
      setTimeout(function () {
        that.setData({
          disable: false,
          countdown:60
        });
      }, 1000);
    }else{
      setTimeout(function(){
        that.cd();
      },1000);
    }
  },
  reset() {
    if (posting) return;
    posting = true;
    setTimeout(function () {
      posting = false;
    }, 1000);
    var that = this;
    valid.forgetpsw(this.data.mobile, this.data.code, this.data.pswnew, this.data.pswnewconfirm, that.data.shopids[that.data.shopindex],{
      success() {
        that.showtip(3, "修改成功");
        setTimeout(function () {
          wx.navigateBack({})
        }, 500);
      }, fail(code, msg) {
        that.showtip(code, msg);
      }, complete(){
        posting = false;
      }
    });
  },
  Confirm: function () {
    this.setData({
      hidden: true
    })
  },
  onLoad: function () {

  }
})
